//
//  UIView+Hud.m
//  AiBangMall
//
//  Created by 邓志平 on 2017/3/22.
//  Copyright © 2017年 DongHui. All rights reserved.
//

#import "UIView+Hud.h"
#import "MBProgressHUD.h"
#import "objc/runtime.h"

@implementation UIView (Hud)

static char LoadingViewKey;

- (void)setLoadingView:(MBProgressHUD *)loadingView {
    [self willChangeValueForKey:@"LoadingViewKey"];
    objc_setAssociatedObject(self, &LoadingViewKey,
                             loadingView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"LoadingViewKey"];
}

- (MBProgressHUD *)loadingView{
    return objc_getAssociatedObject(self, &LoadingViewKey);
}

- (void)showLoading {
    if (!self.loadingView) {
        self.loadingView = [[MBProgressHUD alloc] initWithView:self];
    }
    if (!self.loadingView.superview) {
        [self addSubview:self.loadingView];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.loadingView showAnimated:YES];
    });
}

- (void)hideLoading {
    if (self.loadingView) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.loadingView hideAnimated:YES];
        });
    }
}
@end
